package com.bristua.mall.user.biz;

import com.bristua.mall.common.bean.PageBean;
import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.user.dao.TUserAdressMapper;
import com.bristua.mall.user.dao.TUserCollectionMapper;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserAdress;
import com.bristua.mall.user.model.TUserCollection;
import com.bristua.mall.user.model.condition.TUserAdressExample;
import com.bristua.mall.user.model.condition.TUserCollectionExample;
import com.bristua.mall.user.model.vo.QueryUserAddressVO;
import com.bristua.mall.user.model.vo.QueryUserGoodsCollectionVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/3/25.
 */
@Service
public class QueryUserGoodsCollectionBiz {

    @Autowired
    private CacheUtil cacheUtil;

    @Resource
    private TUserCollectionMapper tUserCollectionMapper;

    public PageBean queryUserGoodsCollection(int pageNo, int pageSize){
        TUser userSession = (TUser) cacheUtil.getUserSession();
        TUserCollectionExample tUserCollectionExample = new TUserCollectionExample();
        tUserCollectionExample.createCriteria().andUserIdEqualTo(userSession.getUserId());//用户ID
        Page<TUserAdress> objects = PageHelper.startPage(pageNo,pageSize);
        List<TUserCollection> tUserCollections = tUserCollectionMapper.selectByExample(tUserCollectionExample);
        PageBean data = new PageBean<>(objects);
        if(data.getList()!=null && data.getList().size()>0){
            List<QueryUserGoodsCollectionVO> queryUserGoodsCollectionVOS = new ArrayList<>();
            for(int i=0;i<data.getList().size();i++){
                TUserCollection tUserCollection = (TUserCollection) data.getList().get(i);
                QueryUserGoodsCollectionVO queryUserGoodsCollectionVO = new QueryUserGoodsCollectionVO();
                BeanUtils.copyProperties(tUserCollection,queryUserGoodsCollectionVO);
                queryUserGoodsCollectionVOS.add(queryUserGoodsCollectionVO);
//                tDevice.setIsOnline(DeviceCode.getValueByCode(Integer.valueOf(tDevice.getIsOnline())));
            }
            data.getList().clear();
            data.getList().addAll(queryUserGoodsCollectionVOS);
        }
        return data;
    }

}
